package com.zbb.comm.service.impl;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbb.base.model.Result;
import com.zbb.comm.controller.WebSocketServer;
import com.zbb.comm.feignclient.UserServiceClient;
import com.zbb.comm.mapper.MessageMapper;
import com.zbb.comm.po.Message;
import com.zbb.comm.po.OrderSnapshot;
import com.zbb.comm.po.User;
import com.zbb.comm.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    WebSocketServer webSocketServer;

    @Autowired
    UserServiceClient userServiceClient;

    @Override
    public int saveMessage(Message message) {
        int insert = messageMapper.insert(message);
        return insert;
    }

    @Override
    public List<Message> selectMessageList(String receivUserId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiv_user_id",receivUserId);
        List<Message> messages = messageMapper.selectList(queryWrapper);
        return messages;
    }

    @Override
    public int deleteMessageList(String receivUserId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiv_user_id",receivUserId);
        int delete = messageMapper.delete(queryWrapper);
        return delete;
    }

    @Override
    public Result commUserMessage(OrderSnapshot orderSnapshot) {
        if (webSocketServer.sessionMap.containsKey(orderSnapshot.getGroupUserCode())){
            User user = userServiceClient.querytUser(orderSnapshot.getHelpUserCode());
            orderSnapshot.setHelpUserName(user.getUserName());
            JSONObject result = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("orderSnapshot", orderSnapshot);
            result.set("commGroupUser", jsonObject);
            webSocketServer.sendMessage(JSON.toJSONString(result), webSocketServer.sessionMap.get(orderSnapshot.getGroupUserCode()));
            return Result.ok();
        }else {
            return Result.ok();
        }
    }

    @Override
    public Result commHelpUserMessage(OrderSnapshot orderSnapshot) {
        if (webSocketServer.sessionMap.containsKey(orderSnapshot.getHelpUserCode())){
            JSONObject result = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("orderSnapshot", orderSnapshot);
            result.set("commGroupHelpUser", jsonObject);
            webSocketServer.sendMessage(JSON.toJSONString(result), webSocketServer.sessionMap.get(orderSnapshot.getHelpUserCode()));
            return Result.ok();
        }else {
            return Result.ok();
        }
    }
}
